<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/14/014
 * Time: 10:20
 */

namespace app\api\model;


use app\api\service\Token;
use app\lib\exception\MemberException;

use app\api\model\Member as MemberModel;

class MemberBill extends BaseModel {
    protected $hidden = ['create_time', 'update_time'];

    /**
     * 获取账单列表
     * @return array
     */
    public static function detailList() {
        $uid = Token::getCurrentUid();
        $member_bill = self::where('member_id', '=', $uid)->order('id desc')->paginate();
        $data = [];

        $week_array = ["周日","周一","周二","周三","周四","周五","周六"];

        $money_type_array = ['', '余额', '衣币', '现金'];

        foreach ($member_bill as $k => $v) {
            if ($v['type'] == 1 || $v['type'] == 6 || $v['type'] == 4 || $v['type'] == 8) {
                $modified = '-';
            } else {
                $modified = '+';
            }

            if (date('Y-m-d') == date('Y-m-d', $v['create_time'])) {
                $day = '今天';
            } else if (date('Y-m-d', strtotime("-1 day")) == date('Y-m-d', $v['create_time'])) {
                $day = '昨天';
            } else {
                $day = $week_array[date("w", $v['create_time'])];
            }

            $key = date('Y-m');
            $data['data'][$key]['m'] = date('n');
            $data['data'][$key]['list'][] = [
                'id' => $v['id'],
                'create_time' => $v['create_time'],
                'week' => $day,
                'money' => $modified . $v['money'],
                'money_type' => $money_type_array[$v['money_type']],
                'msg' => $v['msg'],
                'type' => $v['type'],
                'type_4_status' => self::getMemberBillType4Status($v['type_4_status'])
            ];
        }

        return $data;
    }

    /**
     * 获取用户账单详情
     * @param $id
     * @return static
     * @throws MemberException
     */
    public static function detailInfo($id) {
        $uid = Token::getCurrentUid();
        $member_bill = self::get(['member_id' => $uid, 'id' => $id]);
        if (!$member_bill) {
            throw new MemberException([
                'msg' => '用户账单明细不存在',
                'errorCode' => 30001
            ]);
        }
        if ($member_bill['type'] == 1 || $member_bill['type'] == 6 || $member_bill['type'] == 4 || $member_bill['type'] == 8) {
            $modified = '-';
        } else {
            $modified = '+';
        }
        $member_bill['money'] = $modified . $member_bill['money'];

        $money_type_array = ['', '余额', '衣币', '现金'];
        $member_bill['money_type'] = $money_type_array[$member_bill['money_type']];

        return $member_bill;
    }

    /**
     * 获取提现账单状态信息
     * @param $status
     * @return mixed
     */
    private static function getMemberBillType4Status($status) {
        $status_array = [
            '',
            '<span class="fs06 cl-ffa">待审核</span>',
            '<span class="fs06 cl-31a">已完成</span>',
            '<span class="fs06 cl-ff0">审核失败</span>'];
        return $status_array[$status];
    }

    /**
     * 添加账单
     * @param $member_id (用户ID)
     * @param $order_price (账单金额
     * @param $msg (账单说明
     * @param $type (账单类型
     * @param $order_sn (订单号
     * @param $model (订单模型
     * @param $pay_type (支付方式
     * @param int $money_type  账单金额类型
     * @return bool
     */
    public static function add_member_bill($member_id, $order_price, $msg, $type, $order_sn, $model, $pay_type, $money_type = 1)
    {
        // 获取用户角色的余额
        $member = MemberModel::get(['id' => $member_id]);
        // 增加账单
        $member_money = [
            'member_id' => $member_id, // 用户ID
            'money' => $order_price, // 账单金额
            'money_type' => $money_type, // 账单金额
            'msg' => $msg, // 账单说明
            'type' => $type, // 资金来源 1订单支付   2充值  3订单分成
            'order_sn' => $order_sn, // 订单号
            'order_model' => $model, // 订单模型
            'pay_type' => $pay_type, // 交易方式
            'remaining' => $member['money'], // 用户角色余额
            'residue_clothes_money_recharge' => $member['clothes_money_recharge'], // 用户角色剩余充值衣币
            'residue_clothes_money_donate' => $member['clothes_money_donate'], // 用户角色剩余赠送衣币
            'create_time' => time(),
            'update_time' => time()
        ];
        if ($type == 4) {
            $member_money['type_4_status'] = 1;
        }
        self::create($member_money); // 增加账单

        return true;
    }
}